﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KaoWuYou.Models;
using KaoWuYouLibs.Bll;

namespace KaoWuYou.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        GetListsBusiness getListBusiness = new GetListsBusiness();
        TestPaperManagementBusiness testPaperManagementBusiness = new TestPaperManagementBusiness();

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            model.Subjects = getListBusiness.GetSubjectsAndInitSubclasses();
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TestPaperList(string SubjectID, string SubclassID, string Page)
        {
            TestPaperListModel model = new TestPaperListModel()
            {
                TestPaperModels = testPaperManagementBusiness.GetTestPaperListBySubjectIDAndSubclassID(
                Convert.ToInt32(SubjectID), Convert.ToInt32(SubclassID)),
                SubjectID = Convert.ToInt32(SubjectID),
                SubclassID = Convert.ToInt32(SubclassID)
            };
            int page = Convert.ToInt32(Page);
            if (page <= 0)
                model.Page = 1;
            else if (model.TestPaperModels == null)
                model.Page = 1;
            else if (page > (model.TestPaperModels.Count - 1) / 10 + 1)
                model.Page = (model.TestPaperModels.Count - 1) / 10 + 1;
            else
                model.Page = Convert.ToInt32(Page);

            return View(model);
        }

        public ActionResult Test(string TestPaperID)
        {
            TestModel model = new TestModel();
            model.TestPaperModel = testPaperManagementBusiness.GetTestPaperDetails(Convert.ToInt32(TestPaperID));
            model.CurrentQuestion = 0;
            model.Answers = new string[model.TestPaperModel.Questions.Count];
            model.CurrentGenre = "";
            model.CurrentAnswer = "";

            Session["Paper"] = model;
            return RedirectToAction("Tests");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Tests()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Tests(TestModel model)
        {
            model = (TestModel)Session["Paper"];
            string genre = model.TestPaperModel.Questions[model.CurrentQuestion].Genre;
            string answer = "";
            switch (genre)
            {
                case "选择题":
                    answer += Request.Form["SingleAnswer"];
                    break;
            }
            model.Answers[model.CurrentQuestion] = answer;

            if (Request.Form["Next"] != null)
                model.CurrentQuestion = model.CurrentQuestion + 1 >= model.TestPaperModel.Questions.Count ? model.TestPaperModel.Questions.Count - 1 : model.CurrentQuestion + 1;
            if (Request.Form["Last"] != null)
                model.CurrentQuestion = model.CurrentQuestion - 1 < 0 ? 0 : model.CurrentQuestion - 1;
            if (Request.Form["Submit"] != null)
            {
                Session["Paper"] = model;
                return RedirectToAction("Score");
            }
            model.CurrentGenre = model.TestPaperModel.Questions[model.CurrentQuestion].Genre;
            model.CurrentAnswer = model.Answers[model.CurrentQuestion] == null ? "" : model.Answers[model.CurrentQuestion];

            Session["Paper"] = model;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Score()
        {
            TestModel model = (TestModel)Session["Paper"];
            ScoreModel re = new ScoreModel();
            int testScore = 0;
            int totalScore = model.TestPaperModel.TotalMark;
            for (int i = 0; i < model.Answers.Length; i++)
            {
                if (model.Answers[i].Equals(model.TestPaperModel.Questions[i].AnswerKey))
                    testScore += model.TestPaperModel.Questions[i].Mark;
            }
            re.TestScore = testScore;
            re.TotalScore = totalScore;
            re.TestPaperName = model.TestPaperModel.TestPaperName;
            return View(re);
        }
    }
}
